package com.boguan.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 短信验证码生成工具
 */
@Component
public class SMSVerification {

    Logger logger = LoggerFactory.getLogger(SMSVerification.class);

    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.accessSecret}")
    private String accessSecret;


    /**
     * 短信验证码消息发送
     *
     * @param signName
     * @param phoneNumbers
     * @param TemplateCode
     * @return
     */
    public boolean SMSSendVerificationCode(String signName, String phoneNumbers, String TemplateCode, String code) {
        return sendSms(signName, phoneNumbers, TemplateCode, "{\"code\":\"" + code + "\"}");
    }


    /**
     * 报警短息推送
     *
     * @param signName
     * @param phoneNumbers
     * @param TemplateCode
     * @param message
     * @return
     */
    public boolean sendAlarm(String signName, String phoneNumbers, String TemplateCode, String message) {
        return sendSms(signName, phoneNumbers, TemplateCode, message);
    }

    private boolean sendSms(String signName, String phoneNumbers, String TemplateCode, String TemplateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", TemplateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);

            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            logger.info("验证码发送返回:{}", jsonObject);
            if ("OK".equals(jsonObject.getString("Code")) && "OK".equals(jsonObject.getString("Message"))) {
                return true;
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    public void setAccessSecret(String accessSecret) {
        this.accessSecret = accessSecret;
    }
}
